#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
#include <QStringRef>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //glWidget = new GLWidget;
    m_protein.insert("UUU","F");
    m_protein.insert("UUC","F");

    m_protein.insert("UUA", "L");
    m_protein.insert("UUG", "L");

    m_protein.insert("CUU", "L");
    m_protein.insert("CUC", "L");
    m_protein.insert("CUA", "L");
    m_protein.insert("CUG", "L");

    m_protein.insert("AUU", "I");
    m_protein.insert("AUC", "I");
    m_protein.insert("AUA", "I");

    m_protein.insert("AUG", "M");

    m_protein.insert("GUU", "V");
    m_protein.insert("GUC", "V");
    m_protein.insert("GUA", "V");
    m_protein.insert("GUG", "V");

    m_protein.insert("UCU", "S");
    m_protein.insert("UCC", "S");
    m_protein.insert("UCA", "S");
    m_protein.insert("UCG", "S");

    m_protein.insert("CCU", "P");
    m_protein.insert("CCC", "P");
    m_protein.insert("CCA", "P");
    m_protein.insert("CCG", "P");

    m_protein.insert("ACU", "T");
    m_protein.insert("ACC", "T");
    m_protein.insert("ACA", "T");
    m_protein.insert("ACG", "T");

    m_protein.insert("GCU", "A");
    m_protein.insert("GCC", "A");
    m_protein.insert("GCA", "A");
    m_protein.insert("GCG", "A");

    m_protein.insert("UAU", "Y");
    m_protein.insert("UAC", "Y");

    m_protein.insert("CAU", "H");
    m_protein.insert("CAC", "H");
    m_protein.insert("CAA", "Q");
    m_protein.insert("CAG", "Q");

    m_protein.insert("AAU", "N");
    m_protein.insert("AAC", "N");
    m_protein.insert("AAA", "K");
    m_protein.insert("AAG", "K");

    m_protein.insert("GAU", "D");
    m_protein.insert("GAC", "D");
    m_protein.insert("GAA", "E");
    m_protein.insert("GAG", "E");


    m_protein.insert("UGU", "C");
    m_protein.insert("UGC", "C");

    m_protein.insert("UGG", "W");

    m_protein.insert("CGU", "R");
    m_protein.insert("CGC", "R");
    m_protein.insert("CGA", "R");
    m_protein.insert("CGG", "R");

    m_protein.insert("AGU", "S");
    m_protein.insert("AGG", "S");

    m_protein.insert("AGA", "R");
    m_protein.insert("AGG", "R");

    m_protein.insert("GGU", "G");
    m_protein.insert("GGC", "G");
    m_protein.insert("GGA", "G");
    m_protein.insert("GGG", "G");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_m_clearButton_clicked()
{
    ui->m_textEdit->clear();
    //ui->m_textResult->clear();
    // TODO
    ui->tableWidget->clear();
}

void MainWindow::on_pushButton_clicked()
{
    QString str = ui->m_textEdit->toPlainText();
    QString res;

    if (!ui->m_checkBox->isChecked()) {
        int begin = 0;

        if (ui->m_shiftBox->isChecked()) {
            begin = ui->m_shift->toPlainText().toInt();
        }

        for (int i = begin; i < str.size(); i += 3) {
            QStringRef subString(&str, i, 3);
            if (subString.toString().toUpper() == "UAA" || subString.toString().toUpper() == "UAG" || subString.toString().toUpper() == "UGA")
            {
                res += QString('+');
                continue;
            }
            QMap<QString, QString>::const_iterator i = m_protein.find(subString.toString().toUpper());
            res += i.value();
        }
        ui->tableWidget->setColumnCount(res.size());
        ui->tableWidget->setRowCount(1);
        bool flag = true;
        qDebug() << res.size();
        for (int i = 0; i < res.size(); ++i) {
            QString item = QString(res[i]);
            QTableWidgetItem *newItem = new QTableWidgetItem(item);
            if (res[i] == 'M') {
                QColor c = QColor("green");
                newItem->setTextColor(c);
            }

            if (!flag) {
                QColor c = QColor("gray");
                newItem->setTextColor(c);
            }
            if (res[i] == '+') {
                QColor c = QColor("red");
                newItem->setTextColor(c);
                flag = false;
            }
            ui->tableWidget->setItem(0, i, newItem);
        }
    } else
    {
        int max = 1;
        QList < QList<QString> > tmp;

        for (int i = 0; i < str.size(); i += 1) {
            QStringRef subString(&str, i, 1);
            QMap<QString, QString>::iterator i;
            QList <QString> tmp2;
            for (i = m_protein.begin(); i != m_protein.end(); ++i) {

                QString val = i.value();
                if (val == subString.toString()) {
                    tmp2.append(i.key());
                }
            }
            if (max < tmp2.size()) {
                max = tmp2.size();
            }
            tmp.append(tmp2);
        }

        ui->tableWidget->setColumnCount(tmp.size());
        ui->tableWidget->setRowCount(max);

        QList < QList<QString> >::iterator j;
        for (int k = 0; k < tmp.size(); ++k) {
            for (int l = 0; l < tmp[k].size(); ++l) {
                QString item = QString(tmp[k][l]);
                QTableWidgetItem *newItem = new QTableWidgetItem(item);
                ui->tableWidget->setItem(l, k, newItem);
            }
        }
    }
    ui->tableWidget->resizeColumnsToContents();
}

void MainWindow::on_m_shiftBox_clicked(bool checked)
{
    ui->m_shift->setEnabled(checked);
}

void MainWindow::on_m_checkBox_clicked(bool checked)
{
    ui->m_shift->setEnabled(!checked);
    ui->m_shiftBox->setEnabled(!checked);
}
